import { computed } from "vue";
export function useComputed(fn) {
  const cache = new Map();
  function getCache(args) {
    const keys = [...cache.keys()];
    const key = keys.find((item) => compare(item, args));
    if (key) {
      return cache.get(key);
    }
  }
  function compare(args1, args2) {
    return (
      args1.length === args2.length &&
      args1.every((item, index) => Object.is(item, args2[index]))
    );
  }
  return function (...args) {
    const cacheResult = getCache(args);
    if (cacheResult) {
      return cacheResult.value;
    }
    const result = computed(() => fn(...args));
    cache.set(args, result);
    return result.value;
  };
}
